package propertymgmt.gui;

import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import propertymgmt.entities.Property;
import propertymgmt.util.PropertiesManager;

/**
 * CreateNewProperty.java
 * 
 * Allows the user to create a new Property.
 *
 * @author Brian
 */
@SuppressWarnings("serial")
public class CreateNewProperty extends javax.swing.JInternalFrame {
	
    /** Creates new form CreateNewProperty */
    public CreateNewProperty() {
        initComponents();
    }
    
    public String getPropertyName(){
    	return txt_propertyName.getText().trim();
    }
    
    public String getPropertyAddr(){
    	return txt_propertyAddress.getText().trim();
    }
    
    public double getSquareFeet(){
    	if(ftxt_squareFeet.getText().trim().isEmpty()) return 0;
    	return Double.parseDouble(ftxt_squareFeet.getText().trim());
    }
    
    public int getNumRooms(){
    	if(ftxt_numRooms.getText().trim().isEmpty()) return 0;
    	return Integer.parseInt(ftxt_numRooms.getText().trim());
    }
    
    public int getNumBaths(){
    	if(ftxt_numBaths.getText().trim().isEmpty()) return 0;
    	return Integer.parseInt(ftxt_numBaths.getText().trim());
    }
    
    public int getNumHalfBaths(){
    	if(ftxt_numHalfBaths.getText().trim().isEmpty()) return 0;
    	return Integer.parseInt(ftxt_numHalfBaths.getText().trim());
    }
    
    public String getExtraInfo(){
    	return jtxt_extraInfo.getText().trim();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        propertyInfoPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txt_propertyName = new javax.swing.JTextField();
        txt_propertyAddress = new javax.swing.JTextField();
        ftxt_numHalfBaths = new javax.swing.JFormattedTextField();
        ftxt_numBaths = new javax.swing.JFormattedTextField();
        ftxt_numRooms = new javax.swing.JFormattedTextField();
        ftxt_squareFeet = new javax.swing.JFormattedTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jtxt_extraInfo = new javax.swing.JTextArea();
        jPanel5 = new javax.swing.JPanel();
        btn_save = new javax.swing.JButton();
        btn_close = new javax.swing.JButton();
        
        txt_propertyName.setHorizontalAlignment(JTextField.CENTER);
        txt_propertyAddress.setHorizontalAlignment(JTextField.CENTER);
        ftxt_squareFeet.setHorizontalAlignment(JFormattedTextField.CENTER);
        ftxt_numRooms.setHorizontalAlignment(JFormattedTextField.CENTER);
        ftxt_numBaths.setHorizontalAlignment(JFormattedTextField.CENTER);
        ftxt_numHalfBaths.setHorizontalAlignment(JFormattedTextField.CENTER);

        setClosable(true);
        setIconifiable(true);
        setTitle("Create New Property");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        propertyInfoPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Property Name");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 14, -1, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Property Address");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Square Feet");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 86, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Number of Rooms");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 122, -1, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Number of Bathrooms");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 158, -1, -1));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("Number of Half Bathrooms");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 194, -1, -1));
        jPanel1.add(txt_propertyName, new org.netbeans.lib.awtextra.AbsoluteConstraints(199, 12, 440, -1));
        jPanel1.add(txt_propertyAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(199, 48, 440, -1));

        ftxt_numHalfBaths.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("###"))));
        jPanel1.add(ftxt_numHalfBaths, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 192, 440, -1));

        ftxt_numBaths.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("###"))));
        jPanel1.add(ftxt_numBaths, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 156, 440, -1));

        ftxt_numRooms.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("###"))));
        jPanel1.add(ftxt_numRooms, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 118, 440, -1));

        ftxt_squareFeet.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("###0.##"))));
        jPanel1.add(ftxt_squareFeet, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 84, 440, -1));

        jPanel2.add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 660, 230));

        propertyInfoPanel.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 680, 250));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Extra Information", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jScrollPane4.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jtxt_extraInfo.setColumns(20);
        jtxt_extraInfo.setRows(5);
        jtxt_extraInfo.setWrapStyleWord(true);
        jScrollPane4.setViewportView(jtxt_extraInfo);

        jPanel3.add(jScrollPane4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 640, 130));

        propertyInfoPanel.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 250, 660, 160));

        tabbedPane.addTab("Property Info", propertyInfoPanel);

        getContentPane().add(tabbedPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 690, 440));

        btn_save.setText("Save");
        btn_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_saveActionPerformed(evt);
            }
        });

        btn_close.setText("Close");
        btn_close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_closeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(524, Short.MAX_VALUE)
                .addComponent(btn_save, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(btn_close, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_save)
                    .addComponent(btn_close))
                .addContainerGap())
        );

        getContentPane().add(jPanel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 440, 700, 50));

        pack();
    }// </editor-fold>

    private void btn_closeActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
    }

    private void btn_saveActionPerformed(java.awt.event.ActionEvent evt) {
        if(checkFields()){
        	Property newProperty = new Property(getPropertyName(), getPropertyAddr(), getSquareFeet(),
        			getNumRooms(), getNumBaths(), getNumHalfBaths(), getExtraInfo());
        	
        	PropertiesManager.getInstance().newProperty(newProperty);
        	PropertiesManager.getInstance().saveDbState();
        	
        	clearFields();
        }
    }
    
    private boolean checkFields(){
    	boolean passed = !txt_propertyName.getText().trim().isEmpty();
    	
    	passed &= !txt_propertyAddress.getText().trim().isEmpty();
    	
    	if(!passed){
    		JOptionPane.showMessageDialog(this, "Please fill in at least the name and address of the property.");
    	}
    	
    	return passed;
    }
    
    private void clearFields(){
    	txt_propertyName.setText(null);
    	txt_propertyAddress.setText(null);
    	ftxt_squareFeet.setText(null);
    	ftxt_numRooms.setText(null);
    	ftxt_numBaths.setText(null);
    	ftxt_numHalfBaths.setText(null);
    	jtxt_extraInfo.setText(null);
    }


    // Variables declaration - do not modify
    private javax.swing.JButton btn_close;
    private javax.swing.JButton btn_save;
    private javax.swing.JFormattedTextField ftxt_numBaths;
    private javax.swing.JFormattedTextField ftxt_numHalfBaths;
    private javax.swing.JFormattedTextField ftxt_numRooms;
    private javax.swing.JFormattedTextField ftxt_squareFeet;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea jtxt_extraInfo;
    private javax.swing.JPanel propertyInfoPanel;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JTextField txt_propertyAddress;
    private javax.swing.JTextField txt_propertyName;
    // End of variables declaration

}
